package com.qzsf.backend.controller;

import com.qzsf.backend.common.Result;
import com.qzsf.backend.common.VerificationCodeGenerator;
import com.qzsf.backend.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/12/3 15:01 周二
 * @Email 1429278462@qq.com
 * @Remark 邮箱发送验证码控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("email")
public class EmailController {
    @Autowired
    private EmailService emailService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping
    public Result sendEmail(@RequestParam String email){
        stringRedisTemplate.delete(email);
        String code = VerificationCodeGenerator.generateCode();
        emailService.sendVerificationCode(email,code);
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set(email,code,5, TimeUnit.MINUTES);
        return Result.success();
    }
}
